package com.enjoy.nio.buffer;

import java.nio.ByteBuffer;

public class BufferMain1 {
    /*一次性get*/
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        byteBuffer.put((byte) 'a');
        byteBuffer.put((byte) 'b');
        byteBuffer.put((byte) 'c');
        byteBuffer.put((byte) 'd');
        byteBuffer.put((byte) 'e');

        System.out.println("put a b c d e---> " + byteBuffer);
        // 切换为读模式
        byteBuffer.flip();
        byte[] bytes = new byte[byteBuffer.remaining()];

        System.out.println("before get() ---> " + byteBuffer);
        /*一次性拿完数据*/
        byteBuffer.get(bytes, 0, bytes.length);
        System.out.println("bytes ---> " + new String(bytes));
        System.out.println("after get() ---> " + byteBuffer);
    }
}
